package com.biz;

import java.util.Set;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.biz.global.BizAppBindIntercept;
import com.biz.global.BizGlobalIntercept;
import com.biz.global.BizMetaIntercept;
import com.eova.interceptor.JParaInterceptor;
import com.jfinal.config.Constants;
import com.jfinal.config.Interceptors;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.mysql.jdbc.Connection;
import com.oss.config.DemoMoudleConfig;
import com.rock.app.config.AppMoudleConfig;
import com.rock.sys.config.RockConfig;
import com.rock.sys.config.RockConst;


public class BizDemoConfig extends RockConfig {

	/**
	 * 常量配置
	 * 自定义子模块的配置类
	 */
	@Override
	public void configConstant(Constants me) {
		super.configConstant(me);
		
		//设置子模块的配置类信息
//    	addConfig(OnLineMoudleConfig.class);
    	addConfig(AppMoudleConfig.class);
    	addConfig(DemoMoudleConfig.class);
    	
	}
	
    /**
     * 自定义路由
     *
     * @param me
     */
    @Override
    protected void route(Routes me) {
    	//务必首先执行父类方法
    	super.route(me);
    	
        // 自定义的路由配置往这里加。。。
        me.add("/", BizDemoController.class);
        //end
    }

    /**
     * 自定义Main数据源Model映射
     *
     * @param arp
     */
    @Override
    protected void mapping(ActiveRecordPlugin arp) {
    	//务必首先执行父类方法
        super.mapping(arp);
        
        // 自定义业务Model映射往这里加
    }

    /**
     * 自定义插件
     */
    @Override
    protected void plugin(Plugins plugins) {
    	//务必首先执行父类方法
    	super.plugin(plugins);
    	
        /** 新增自定义数据源start **/
        // ActiveRecordPlugin arp = addDataSource(plugins, "oss", JdbcUtils.MYSQL);
        // arp.addMapping("xxx", Xxx.class);
        /** 新增自定义数据源end **/

        // 添加自动扫描插件

        // ...
    }

    /**
     * 新增自定义数据源
     *
     * @param plugins
     * @param datasource
     * @param dbType
     * @return
     */
    @SuppressWarnings("unused")
    private ActiveRecordPlugin addDataSource(Plugins plugins, String datasource, String dbType) {

        // 添加数据源
        String url, user, pwd;
        url = props.get(datasource + "_url");
        user = props.get(datasource + "_user");
        pwd = props.get(datasource + "_pwd");

        WallFilter wall = new WallFilter();
        wall.setDbType(dbType);

        DruidPlugin dp = new DruidPlugin(url, user, pwd);
        dp.addFilter(new StatFilter());
        dp.addFilter(wall);

        ActiveRecordPlugin arp = new ActiveRecordPlugin(datasource, dp);
        // 方言
        arp.setDialect(new MysqlDialect());
        // 事务级别
        arp.setTransactionLevel(Connection.TRANSACTION_REPEATABLE_READ);
        // 统一全部默认小写
        arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
        // 是否显示SQL
        arp.setShowSql(true);
        System.out.println("load data source:" + url + "/" + user);

        plugins.add(dp).add(arp);

        // 注册数据源
        dataSources.add(datasource);

        return arp;
    }

    @Override
	public void configInterceptor(Interceptors me) {
    	me.add(new JParaInterceptor());
    	
    	//单点登录
//    	me.add(new SinglepointInterception());
    	
		super.configInterceptor(me);
	}

	/**
     * 自定义表达式(主要用于级联)
     */
    @Override
    protected void exp() {
        super.exp();
        // 区域级联查询
//        exps.put("selectAreaByLv2AndPid", "select id ID,name CN from S_T_CITY where leveltype = 2 and status=1 and parentid = ?");
//        exps.put("selectAreaByLv3AndPid", "select id ID,name CN from S_T_CITY where leveltype = 3 and status=1 and parentid = ?");
        
        
        //exps.put("selectAreaByLv2AndPid", "select id ID,name CN from [area] where lv = 2  and pid = ?");
        //exps.put("selectAreaByLv3AndPid", "select id ID,name CN from [area] where lv = 3  and pid = ?");
        // 用法，级联动态在页面改变SQL和参数
        // $xxx.eovacombo({exp : 'selectAreaByLv2AndPid,aaa,10'}).reload();
        // $xxx.eovafind({exp : 'selectAreaByLv2AndPid,aaa,10'});
    }

    @Override
    protected void auth() {
    	//务必首先执行父类方法
        super.auth();
        
        //此处设置需要用户登录的所有公共权限
        //将首页和图表设置为公共角色权限
        Set<String> uris = this.auths.get(0);
        this.auths.put(0, uris);
    }
    

	@Override
	public void configEova() {
		super.configEova();
		
		/*
		 * 自定义Eova全局拦截器
		 * 全局的查询拦截,可快速集中解决系统的查询数据权限,严谨,高效!
		 */
		setEovaIntercept(new BizGlobalIntercept());
		/*
		 * 默认元对象业务拦截器:未配置元对象业务拦截器会默认命中此拦截器
		 * 自定义元对象拦截器时自行考虑是否需要继承默认拦截器
		 */
		setDefaultMetaObjectIntercept(new BizMetaIntercept());
		
		//设置默认角色绑定拦截器
		setDefaultRoleBindIntercept(new BizAppBindIntercept());
	}
	


}